package com.example.farmeri.ui.news;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.farmeri.R;
import com.example.farmeri.pojo.Message;
import com.example.farmeri.pojo.Messages;
import com.example.farmeri.tools.DataUtils;

import java.util.ArrayList;

public  class NewsManageAdapter extends BaseAdapter {
    Context context;
    OnExtraClickListenerNews onExtraClickListenerNews;
    private Integer screeWidth;
    private ArrayList<Message> messageArray;
    private Integer messageCount;

    public NewsManageAdapter(Context context, int screeWidth, Messages messages) {
        this.context = context;
        this.screeWidth = screeWidth;
        this.messageArray = messages.getMessageListTop();
        this.messageCount = messages.getMessageListCount();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View itemView = inflater.inflate(R.layout.item_news_manager,viewGroup,false);
        return new MyViewHolder(itemView);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        MyViewHolder holder = (MyViewHolder) viewHolder;
        holder.container.smoothScrollTo(0,0);
        holder.layoutShow.setMinWidth(screeWidth);

        Message message = messageArray.get(i);
        holder.text_news_title.setText(message.getTitle());
        holder.text_news_content.setText(message.getContent());
        String date = DataUtils.getStringDate(message.getUpdateDate());
        holder.text_news_date.setText(date);
        holder.checkBox.setChecked(false);
        if ("已读".equals(message.getLooked())){
            holder.img_read.setVisibility(View.GONE);
        }
        //设置滑动效果
        holder.container.setOnTouchListener(getHorizonSlideListener(holder.layoutHide,holder.container));
        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //获取当前的item位置
                int position = holder.getAdapterPosition();
                if (isChecked){
                    onExtraClickListenerNews.onChecked(position,message.getId());
                }else {
                    onExtraClickListenerNews.onUnChecked(position,message.getId());
                }

            }
        });

        //设置点击界面跳转
        holder.layoutShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,NewscheckActivity.class);
                intent.putExtra("title",message.getTitle());
                intent.putExtra("content",message.getContent());
                intent.putExtra("messageType",message.getMessageType());
                intent.putExtra("remarks",message.getRemarks());
                intent.putExtra("date",date);
                context.startActivity(intent);
            }
        });
        //点击已读
        holder.read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onExtraClickListenerNews != null){
                    ((MyViewHolder) viewHolder).img_read.setVisibility(View.GONE);
                    onExtraClickListenerNews.onReadClick(v,holder.getAdapterPosition(),message.getId());
                }

            }
        });
        //点击删除
        holder.del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onExtraClickListenerNews != null) {
                    onExtraClickListenerNews.onDelClick(v,holder.getAdapterPosition(),message.getId());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return messageArray.size();
    }

    public void setonExtraClickListenerNews(OnExtraClickListenerNews onExtraClickListenerNews) {
        this.onExtraClickListenerNews = onExtraClickListenerNews;
    }

    public void notifyMessageRead(String s) {

    }


    public class MyViewHolder extends RecyclerView.ViewHolder {

        TextView text_news_title;   //消息列表的消息标题
        TextView text_news_content;   //消息列表的消息内容
        TextView text_news_date;   //消息列表的消息内容
        HorizontalScrollView container; //显示界面与操作界面的容器
        ConstraintLayout layoutShow;    //显示界面
        LinearLayout layoutHide;    //操作界面
        CheckBox checkBox;    //复选框
        ImageView img_read; //未读图片
        TextView del;
        TextView read;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
//                relativeLayout = (RelativeLayout) itemView;
            text_news_title =itemView.findViewById(R.id.text_newstitle_item);
            container = itemView.findViewById(R.id.item_news_manager_container);
            layoutShow = itemView.findViewById(R.id.item_news_manager_layout_show);
            layoutHide = itemView.findViewById(R.id.item_news_manager_layout_hide);
            checkBox = itemView.findViewById(R.id.item_news_manager_check);
            read = itemView.findViewById(R.id.item_news_manager_read);
            img_read = itemView.findViewById(R.id.news_manager_readimg);
            del = itemView.findViewById(R.id.item_news_manager_del);
            text_news_content = itemView.findViewById(R.id.text_newscontent_item);
            text_news_date = itemView.findViewById(R.id.text_newstime_item1);
//                text_newscontent_item=(TextView) itemView;
//                text_newstime_item1=(TextView) itemView;
//                text_newstime_item2=(TextView) itemView;

        }
    }
}

